//
//  PlaySound.m
//  EduMobileTeacher
//
//  Created by zou tianyu on 14-3-28.
//  Copyright (c) 2014年 zou tianyu. All rights reserved.
//

#import "PlaySound.h"

@implementation PlaySound
-(id)initForPlayingVibrate
{
  self = [super init];
  if (self) {
    soundID = kSystemSoundID_Vibrate;
  }
  return self;
}

-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName
{
  self = [super init];
  if (self) {
//    NSURL *directoryURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds"];
    NSString *path = [@"/System/Library/Audio/UISounds" stringByAppendingPathComponent:resourceName];
    if (path) {
      SystemSoundID theSoundID;
      OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
      if (error == kAudioServicesNoError) {
        soundID = theSoundID;
      }else {
        
      }
    }
    
  }
  return self;
}

-(id)initForPlayingSoundEffectWith:(NSString *)filename
{
  self = [super init];
  if (self) {
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
    if (fileURL != nil)
    {
      SystemSoundID theSoundID;
      OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
      if (error == kAudioServicesNoError){
        soundID = theSoundID;
      }else {
        
      }
    }
  }
  return self;
}

- (void)play
{
//  NSString *path = [@"/System/Library/Audio/UISounds" stringByAppendingPathComponent:@"sms-received1.caf"];
//  SystemSoundID soundID1;
//  AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)[NSURL URLWithString:path],&soundID1);
//  AudioServicesPlaySystemSound(soundID1);
  AudioServicesPlaySystemSound(soundID);
}

+ (void)playSystemAlert {
  NSString *path = [@"/System/Library/Audio/UISounds" stringByAppendingPathComponent:@"sms-received1.caf"];
  SystemSoundID soundID1;
  AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)[NSURL URLWithString:path],&soundID1);
  AudioServicesPlaySystemSound(soundID1);
}

- (void)dealloc
{
  AudioServicesDisposeSystemSoundID(soundID);
}
@end
